home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue61 / Clinic / Object Browser / PropertyHelper.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-03-29  |  5.4 KB  |  170 lines

  1. unit PropertyHelper;
  2.  
  3. {$ifdef Ver100} { Delphi 3.0x }
  4.   {$define DelphiLessThan5}
  5. {$endif}
  6. {$ifdef Ver110} { C++ Builder 3.0x }
  7.   {$define DelphiLessThan5}
  8. {$endif}
  9. {$ifdef Ver120} { Delphi 4.0x }
  10.   {$define DelphiLessThan5}
  11. {$endif}
  12.  
  13. interface
  14.  
  15. uses
  16.   TypInfo, Forms;
  17.  
  18. function GetObjectProp(Instance: TObject; PropInfo: PPropInfo; MinClass: TClass): TObject;
  19. procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo; Value: TObject);
  20.  
  21. function GetPropValue(Obj: TObject; PropInfo: PPropInfo): String;
  22. procedure SetPropValue(Obj: TObject; PropInfo: PPropInfo; const PropValue: String);
  23.  
  24. function DisplayModalAndFree(Form: TCustomForm): TModalResult;
  25.  
  26. type
  27.   { Set access to an integer }
  28.   TIntegerSet = set of 0..SizeOf(Integer) * 8 - 1;
  29.  
  30. implementation
  31.  
  32. uses
  33.   SysUtils, Graphics, Controls, Menus, Classes;
  34.  
  35. function GetObjectProp(Instance: TObject; PropInfo: PPropInfo; MinClass: TClass): TObject;
  36. begin
  37.   Result := TObject(GetOrdProp(Instance, PropInfo));
  38.   if (Result <> nil) and
  39.      (MinClass <> nil) and
  40.      not (Result is MinClass) then
  41.     Result := nil;
  42. end;
  43.  
  44. procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo; Value: TObject);
  45. begin
  46.   if (Value is GetTypeData(PropInfo.PropType^).ClassType) or
  47.      (Value = nil) then
  48.     SetOrdProp(Instance, PropInfo, Integer(Value));
  49. end;
  50.  
  51. function GetPropValue(Obj: TObject; PropInfo: PPropInfo): String;
  52. var
  53.   AMethod: TMethod;
  54.   AnObject: TObject;
  55.   IntegerSet: TIntegerSet;
  56.   Loop: Integer;
  57. begin
  58.   if PropInfo <> nil then
  59.   begin
  60.     try
  61.       case PropInfo.PropType^.Kind of
  62.         tkInteger:
  63.         begin
  64.           if (PropInfo.Name = 'Color') or (Pos('Color', PropInfo.Name) > 0) then
  65.             Result := ColorToString(TColor(GetOrdProp(Obj, PropInfo)))
  66.           else
  67.           if (PropInfo.Name = 'Cursor') or (PropInfo.Name = 'DragCursor') then
  68.             Result := CursorToString(TCursor(GetOrdProp(Obj, PropInfo)))
  69.           else
  70.           if PropInfo.Name = 'ShortCut' then
  71.             Result := ShortCutToText(GetOrdProp(Obj, PropInfo))
  72.           else
  73.             Result := IntToStr(GetOrdProp(Obj, PropInfo))
  74.         end;
  75.         tkChar, tkWChar: Result := Chr(GetOrdProp(Obj, PropInfo));
  76.         tkSet:
  77.         //Result := GetSetProp(Obj, PropInfo, True);
  78.         begin
  79.           Integer(IntegerSet) := GetOrdProp(Obj, PropInfo);
  80.           Result := '';
  81.           for Loop := 0 to SizeOf(Integer) * 8 - 1 do
  82.             if Loop in IntegerSet then
  83.             begin
  84.               if Result <> '' then
  85.                 Result := Result + ',';
  86.               Result := Result + GetEnumName(GetTypeData(PropInfo.PropType^).CompType^, Loop);
  87.             end;
  88.           Result := '[' + Result + ']';
  89.         end;
  90.         tkClass:
  91.         begin
  92.           AnObject := TObject(GetOrdProp(Obj, PropInfo));
  93.           if Assigned(AnObject) and
  94.              (AnObject is TComponent) and
  95.              (TComponent(AnObject).Name <> '') then
  96.             Result := TComponent(AnObject).Name
  97.           else
  98.             Result :=
  99.               '(' + GetTypeData(PropInfo.PropType^)^.ClassType.ClassName + ')';
  100.         end;
  101.         tkEnumeration:
  102.           Result :=
  103.             GetEnumName(PropInfo.PropType^, GetOrdProp(Obj, PropInfo));
  104.         tkFloat:
  105.           Result :=
  106.             FloatToStr(GetFloatProp(Obj, PropInfo));
  107.         tkString, tkLString, tkWString:
  108.           Result := GetStrProp(Obj, PropInfo);
  109.         tkMethod:
  110.         begin
  111.           AMethod := GetMethodProp(Obj, PropInfo);
  112.           if AMethod.Code = nil then
  113.             Result := ''
  114.           else
  115.             //If the method was not published, it's name will
  116.             //not be available, so get its address instead
  117.             try
  118.               Result :=
  119.                 (TObject(AMethod.Data) as TComponent).MethodName(AMethod.Code)
  120.             except
  121.               Result := Format('$%p', [AMethod.Code]);
  122.             end
  123.         end
  124.       end
  125.     except
  126.       Result := '<Error>';
  127.     end
  128.   end
  129. end;
  130.  
  131. procedure SetPropValue(Obj: TObject; PropInfo: PPropInfo; const PropValue: String);
  132. begin
  133.   case PropInfo.PropType^.Kind of
  134.     tkInteger:
  135.     begin
  136.       if (PropInfo.Name = 'Color') or (Pos('Color', PropInfo.Name) > 0) then
  137.         SetOrdProp(Obj, PropInfo, Integer(StringToColor(PropValue)))
  138.       else
  139.       if (PropInfo.Name = 'Cursor') or (PropInfo.Name = 'DragCursor') then
  140.         SetOrdProp(Obj, PropInfo, Integer(StringToCursor(PropValue)))
  141.       else
  142.       if PropInfo.Name = 'ShortCut' then
  143.         SetOrdProp(Obj, PropInfo, Integer(TextToShortCut(PropValue)))
  144.       else
  145.         SetOrdProp(Obj, PropInfo, StrToInt(PropValue))
  146.     end;
  147.     tkChar, tkWChar: SetOrdProp(Obj, PropInfo, StrToInt(PropValue));
  148.   {$ifndef DelphiLessThan5}
  149.     tkSet: SetSetProp(Obj, PropInfo, PropValue);
  150.   {$endif}
  151.     tkClass: raise EInvalidOperation.Create('Changing class type properties is not supported');
  152.     tkEnumeration: SetOrdProp(Obj, PropInfo,
  153.       GetEnumValue(PropInfo.PropType^, PropValue));
  154.     tkFloat: SetFloatProp(Obj, PropInfo, StrToFloat(PropValue));
  155.     tkString, tkLString, tkWString: SetStrProp(Obj, PropInfo, PropValue);
  156.     tkMethod: raise EInvalidOperation.Create('Changing method type properties is not supported');
  157.   end
  158. end;
  159.  
  160. function DisplayModalAndFree(Form: TCustomForm): TModalResult;
  161. begin
  162.   try
  163.     Result := Form.ShowModal
  164.   finally
  165.     Form.Free
  166.   end
  167. end;
  168.  
  169. end.
  170.